/*
  学习目标：使用dispatch修改state数据
*/

import { legacy_createStore as createStore } from 'redux';

// 2. reducer通过第二个参数, 接收action对象
const store = createStore(function (state = { count: 0 }, action) {
  // 3. reducer函数通过返回值, 修改state
  // 💥 不可变数据: 新值覆盖旧值
  if (action.type === 'add') {
    return { ...state, count: state.count + 1 };
  }

  if (action.type === 'des') {
    return { ...state, count: state.count - 1 };
  }
  return state;
});

console.log('store.getState()  ----->  ', store.getState()); // 0

// 1. 💥 dispatch(action对象)
// action对象必须有一个type字段
store.dispatch({ type: 'add' });
store.dispatch({ type: 'des' });

console.log('store.getState()  ----->  ', store.getState()); // 0
